﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;

namespace Cashier.utils
{
    public class RightsforControls
    {
        /// <summary>
        /// 按钮权限
        /// </summary>
        /// <param name="rightTable"></param>
        /// <param name="buttons"></param>
        public static void RightsforButton(DataTable rightsTable, Button[] buttons)
        {
            try
            {
                if (rightsTable != null && rightsTable.Rows.Count > 0 && buttons != null && buttons.Length > 0)
                {
                    String rightcode = String.Empty;
                    foreach (Button button in buttons)
                    {
                        if (button.Tag != null && !String.IsNullOrEmpty(button.Tag.ToString()))
                        {
                            rightcode = button.Tag.ToString();
                            DataRow[] rows = rightsTable.Select("code='" + rightcode + "'");
                            if (rows.Length != 1)
                            {
                                if (Config.RIGHTS_BUTTON_STATE.Equals("hidden"))
                                    button.Visible = false;
                                else
                                    button.Enabled = false;
                            }
                        }
                    }
                }
                else
                {
                    foreach (Button button in buttons)
                    {
                        if (button.Tag != null && !String.IsNullOrEmpty(button.Tag.ToString()))
                        {
                            if (Config.RIGHTS_BUTTON_STATE.Equals("hidden"))
                                button.Visible = false;
                            else
                                button.Enabled = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 右键菜单权限
        /// </summary>
        /// <param name="rightsTable"></param>
        /// <param name="menuStrips"></param>
        public static void RightsforContextMenuStrip(DataTable rightsTable, ContextMenuStrip[] menuStrips)
        {
            try
            {
                if (rightsTable != null && rightsTable.Rows.Count > 0 && menuStrips != null && menuStrips.Length > 0)
                {
                    String rightcode = String.Empty;
                    foreach (ContextMenuStrip lopMenuStrip in menuStrips)
                    {
                        ToolStripItemCollection coll = lopMenuStrip.Items;
                        foreach (ToolStripItem item in coll)
                        {
                            if (item.Text != null && item.Tag != null && !String.IsNullOrEmpty(item.Tag.ToString()))
                            {
                                rightcode = item.Tag.ToString();
                                DataRow[] rows = rightsTable.Select("code='" + rightcode + "'");
                                if (rows.Length != 1)
                                {
                                    if (Config.RIGHTS_BUTTON_STATE.Equals("hidden"))
                                        item.Visible = false;
                                    else
                                        item.Enabled = false;
                                }
                            }
                        }
                    }
                }
                else
                {
                    foreach (ContextMenuStrip lopMenuStrip in menuStrips)
                    {
                        ToolStripItemCollection coll = lopMenuStrip.Items;
                        foreach (ToolStripItem item in coll)
                        {
                            if (item.Text != null && item.Tag != null && !String.IsNullOrEmpty(item.Tag.ToString()))
                            {
                                if (Config.RIGHTS_BUTTON_STATE.Equals("hidden"))
                                    item.Visible = false;
                                else
                                    item.Enabled = false;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
